package com.jxstyle.core.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import com.jxstyle.core.exception.BizException;
import com.jxstyle.core.exception.BizExceptionEnum;

/**
 * 登录失败处理
 * @author weijx
 *
 */
@Component
public class LoginFailureHandler
		extends org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler {

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException, ServletException {
		if (exception.getCause() instanceof BizException) {
			BizException bizException = (BizException) exception.getCause();
			request.getSession().setAttribute("errorMsg", bizException.getMessage());
			request.getSession().setAttribute("code", bizException.getCode());
		} else {
			request.getSession().setAttribute("errorMsg", BizExceptionEnum.LOGIN_PASSWORD_ERROR.getMessage());
			request.getSession().setAttribute("code", BizExceptionEnum.LOGIN_PASSWORD_ERROR.getCode());
		}
		super.onAuthenticationFailure(request, response, exception);
	}
}
